package com.iims.flowable.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.iims.common.annotation.Log;
import com.iims.common.annotation.RedissonLock;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.enums.BusinessType;
import com.iims.flowable.factory.BaseFlowableController;
import com.iims.flowable.flow.vo.FlowNodeResponse;
import com.iims.flowable.flow.vo.TaskRequest;
import com.iims.flowable.flow.vo.TaskResponse;
import com.iims.flowable.flow.vo.TaskUpdateRequest;
import com.iims.flowable.flow.vo.request.TaskBackRequest;
import com.iims.flowable.flow.vo.request.TaskCompleteRequest;
import com.iims.flowable.service.IFlowTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程操作
 */
@Slf4j
@Api(tags = "流程任务")
@ApiSort(4)
@RestController
@RequestMapping("/flowable/task")
public class FlowTaskController extends BaseFlowableController {

    @Autowired
    protected IFlowTaskService flowableTaskService;

    @Deprecated
    //@ApiOperation(value = "修改任务")
    @ApiOperationSupport(order = 10)
    @Log(title = "修改任务", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('flowable:task:update')")
    @PutMapping(value = "/update")
    public AjaxResult update(@Validated @RequestBody TaskUpdateRequest taskUpdateRequest) {
        TaskResponse task = flowableTaskService.updateTask(taskUpdateRequest);
        return AjaxResult.success(task);
    }

    @ApiOperation(value = "删除任务")
    @ApiOperationSupport(order = 11)
    @Log(title = "删除任务", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('flowable:task:delete')")
    @DeleteMapping(value = "/delete/{taskId}")
    public AjaxResult delete(@ApiParam(value = "任务Id", required = true) @PathVariable String taskId) {
        flowableTaskService.deleteTask(taskId);
        return AjaxResult.success();
    }

    @ApiOperation(value = "转办任务")
    @ApiOperationSupport(order = 12)
    @Log(title = "转办任务", businessType = BusinessType.INSERT)
    @PutMapping(value = "/assign")
    public AjaxResult assign(@Validated @RequestBody TaskRequest taskRequest) {
        flowableTaskService.assignTask(taskRequest);
        return AjaxResult.success();
    }

    @ApiOperation(value = "委派任务")
    @ApiOperationSupport(order = 13)
    @Log(title = "委派任务", businessType = BusinessType.INSERT)
    @PutMapping(value = "/delegate")
    public AjaxResult delegate(@Validated @RequestBody TaskRequest taskRequest) {
        flowableTaskService.delegateTask(taskRequest);
        return AjaxResult.success();
    }

    //@ApiOperation(value = "认领任务")
    @ApiOperationSupport(order = 14, ignoreParameters = {"assignee"})
    @Log(title = "认领任务", businessType = BusinessType.INSERT)
    @RedissonLock(lockIndexs = 0, fieldNames = "taskId")
    @PutMapping(value = "/claim")
    public AjaxResult claim(@RequestBody TaskRequest taskRequest) {
        flowableTaskService.claimTask(taskRequest);
        return AjaxResult.success();
    }

    //@ApiOperation(value = "取消认领任务")
    @ApiOperationSupport(order = 15, ignoreParameters = {"assignee"})
    @Log(title = "取消认领任务", businessType = BusinessType.DELETE)
    @PutMapping(value = "/unclaim")
    public AjaxResult unclaim(@RequestBody TaskRequest taskRequest) {
        flowableTaskService.unclaimTask(taskRequest);
        return AjaxResult.success();
    }

    @ApiOperation(value = "提交任务")
    @ApiOperationSupport(order = 16)
    @Log(title = "提交任务", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/submit")
    public AjaxResult submit(@Validated @RequestBody TaskCompleteRequest taskRequest) {
        flowableTaskService.completeTask(taskRequest);
        return AjaxResult.success();
    }

    @ApiOperation(value = "结束任务(终止)")
    @ApiOperationSupport(order = 17, ignoreParameters = {"assignee"})
    @Log(title = "结束任务", businessType = BusinessType.STOP)
    @PutMapping(value = "/stopProcessInstance")
    public AjaxResult stopProcessInstance(@RequestBody TaskRequest taskRequest) {
        flowableTaskService.stopProcessInstance(taskRequest);
        return AjaxResult.success();
    }

    @ApiOperation(value = "查询可回退节点", response = FlowNodeResponse.class)
    @ApiOperationSupport(order = 18)
    @GetMapping(value = "/backNodes/{taskId}")
    public AjaxResult backNodes(@ApiParam(value = "任务Id", required = true) @PathVariable String taskId) {
        List<FlowNodeResponse> datas = flowableTaskService.getBackNodes(taskId);
        return AjaxResult.success(datas);
    }

    @ApiOperation(value = "退回任务")
    @ApiOperationSupport(order = 19)
    @Log(title = "退回任务", businessType = BusinessType.FORCE)
    @PutMapping(value = "/back")
    public AjaxResult back(@Validated @RequestBody TaskBackRequest taskRequest) {
        flowableTaskService.backTask(taskRequest);
        return AjaxResult.success();
    }

    //@ApiOperation(value = "阅读任务(将抄送给本人的任务置为已读,支持批量)")
    @ApiOperationSupport(order = 20)
    @ApiImplicitParam(name = "", value = "任务id数组，逗号分隔")
    @Log(title = "阅读任务", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/read/{taskIds}")
    public AjaxResult read(String[] taskIds) {
        flowableTaskService.readTask(taskIds);
        return AjaxResult.success();
    }
}
